package com.shikong.blog.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shikong.blog.common.Response;
import com.shikong.blog.dao.AdminUserDao;
import com.shikong.blog.domain.dos.BlogSettingDO;
import com.shikong.blog.domain.dos.UserDO;
import com.shikong.blog.domain.mapper.UserMapper;
import com.shikong.blog.model.systemvo.user.QueryUserDetailRspVO;
import com.shikong.blog.model.systemvo.user.UpdateAdminPasswordReqVO;
import com.shikong.blog.service.AdminUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @description: TODO
 **/
@Service
@Slf4j
public class AdminUserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements AdminUserService {

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private AdminUserDao userDao;

    @Override
    public Response updateAdminPassword(UpdateAdminPasswordReqVO updateAdminPasswordReqVO) {
        UserDO userDO = UserDO.builder()
                .password(passwordEncoder.encode(updateAdminPasswordReqVO.getNewPassword()))
                .updateTime(new Date())
                .build();
        userDao.updateAdminPassword(userDO);
        return Response.success();
    }
    public Response<QueryUserDetailRspVO> queryNicknameAvatar() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();

        QueryUserDetailRspVO queryuserRspVO = QueryUserDetailRspVO.builder().username(username).build();

        UserDO userDO = userDao.selectByUsername(username);
        queryuserRspVO.setNickname(userDO.getNickname());
        queryuserRspVO.setAvatar(userDO.getAvatar());
        return Response.success(queryuserRspVO);

    }
}
